package com.example.nio.service.impl;

import com.example.nio.dto.StatisReportDto;
import com.example.nio.vo.AgentServicefall;
import com.example.nio.commons.StatisReport;
import com.example.nio.dao.StatisReportMapper;
import com.example.nio.service.StatisReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StatisReportServiceImpl implements StatisReportService {
    @Autowired
    private StatisReportMapper statisReportMapper;
    @Override
    public AgentServicefall getAgentServicefall(AgentServicefall servicefall) {
        StatisReport statisReport = new StatisReport();
        statisReport.setTimeStart(servicefall.getCreateDate());
        statisReport.setTimeEnd(servicefall.getEndDate());
        statisReport.setAgentId(servicefall.getAgentId());
        AgentServicefall agentServicefall = statisReportMapper.getAgentServicefall(servicefall);
        if (agentServicefall != null){
            agentServicefall.setFirstSolutionRate(agentServicefall.getFirstSolutionRate().scaleByPowerOfTen(2));
            agentServicefall.setInviteContestantRate(agentServicefall.getInviteContestantRate().scaleByPowerOfTen(2));
            agentServicefall.setGoodEvaluateRate(agentServicefall.getGoodEvaluateRate().scaleByPowerOfTen(2));
            agentServicefall.setContestantRate(agentServicefall.getContestantRate().scaleByPowerOfTen(2));
            agentServicefall.setMediumReviewRate(agentServicefall.getMediumReviewRate().scaleByPowerOfTen(2));
        }
        return agentServicefall;
    }

    @Override
    public int add(StatisReport statisReport) {
        return statisReportMapper.add(statisReport);
    }

    @Override
    public Page<StatisReport> findAll(StatisReportDto statisReport) {
        Pageable pageable = PageRequest.of(statisReport.getCurrentPage()-1, statisReport.getPageSize());
        statisReport.setCurrentPage((int) pageable.getOffset());
        List<StatisReport> statisReportList = statisReportMapper.findAll(statisReport);

        return new PageImpl<StatisReport>(statisReportList);
    }


}
